package threading;

import java.util.concurrent.CountDownLatch;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WHY
 * Date: 2022-10-07
 * Time: 8:59
 */
public class demo2 {
    public static void main(String[] args) throws InterruptedException {
        //有10个选手
        CountDownLatch  countdownlatch=new CountDownLatch(10);
        for(int i=0;i<10;i++){
            //创建10个线程来执行任务
            Thread t=new Thread(()->{
                System.out.println("选手出发"+Thread.currentThread().getName());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

                System.out.println("选手到达"+Thread.currentThread().getName());
                //选手撞线
                countdownlatch.countDown();
            });
            t.start();
        }
        //await是进行阻塞等待，会等到所有选手都撞线以后才会解除阻塞
       countdownlatch.await();
        System.out.println("比赛结束");
    }
}
